﻿Imports Business
Imports Entities
Imports DataAccess
Public Class DeudasPendientes

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub DeudasPendientes_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        Titulo()
    End Sub

   
    Private Sub Titulo()
        With gGrilla
            Dim lista = VentaBL.ListPendientes
            If IsNothing(lista) Then
                .DataSource = Nothing
                If gGrilla.Columns.Contains("RecibirPago") Then
                    .Columns("RecibirPago").Visible = False
                End If
                Exit Sub
            End If

            .DataSource = lista
            .Columns("ClienteId").Visible = False
            .Columns("boletaId").Visible = False
            .Columns("ventaid").Visible = False
            .Font = New Drawing.Font("Tahoma", Util.GridFont, FontStyle.Regular, GraphicsUnit.Point)
            Dim ButtonCell As DataGridViewButtonCell = New DataGridViewButtonCell
            ButtonCell.UseColumnTextForButtonValue = True
            Dim ButtonCol As New DataGridViewButtonColumn

            With ButtonCol
                .HeaderText = "Recibir Pago"
                .Name = "RecibirPago"
                .ReadOnly = False
                '.ValueType = Type.GetType("System.Int64")
                .Resizable = False
                .CellTemplate = ButtonCell
                'ButtonCell.Value = "Recibir Pago"
                ButtonCell.UseColumnTextForButtonValue = True

            End With

            If Not gGrilla.Columns.Contains("RecibirPago") Then
                gGrilla.Columns.Add(ButtonCol)
            End If

            .Columns("FechaVenta").HeaderText = "Fecha Venta"
            .Columns("HoraVenta").HeaderText = "Hora Venta"
            .Columns("MontoTotal").HeaderText = "Monto Total"
            .Columns("MontoPendiente").HeaderText = "Monto Pendiente"
            .Columns("FechaPago").HeaderText = "Fecha Pago"
            .Columns("Numero").HeaderText = "Número"

            .Columns("MontoTotal").DefaultCellStyle.Format = "$ #,###,##0"
            .Columns("MontoPendiente").DefaultCellStyle.Format = "$ #,###,##0"

            '.Columns("numero").DisplayIndex = 1
            '.Columns("calle").DisplayIndex = 2
            '.Columns("villa").DisplayIndex = 3
            '.Columns("rut").DisplayIndex = 4
            '.Columns("nombre").DisplayIndex = 5
            '.Columns("fono").DisplayIndex = 6
            '.Columns("fechaventa").DisplayIndex = 7
            '.Columns("horaventa").DisplayIndex = 8
            '.Columns("montototal").DisplayIndex = 9
            '.Columns("montopendiente").DisplayIndex = 10
            '.Columns("fechapago").DisplayIndex = 11
            '.Columns("producto").DisplayIndex = 12
            '.Columns("RecibirPago").DisplayIndex = 13
            For Each col As DataGridViewColumn In .Columns
                col.ReadOnly = True
            Next

        End With
    End Sub

    Private Sub gGrilla_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles gGrilla.Click
        If gGrilla.Rows.IndexOf(gGrilla.CurrentRow) >= 0 Then
            If gGrilla.CurrentCell.OwningColumn.Name = "RecibirPago" Then
                Using Form As New RecibirPago("RecibirPago", gGrilla.Item("montopendiente", gGrilla.CurrentRow.Index).Value)
                    Form.ShowDialog()
                    If Form.DialogResult = Windows.Forms.DialogResult.OK Then
                        Dim pago As New PAGOS
                        With pago
                            .MONTO = Form.GetValue
                            .FORMAPAGO = Form.cFormaPago.SelectedValue
                            .FECHA = Now
                            .TIPO = Util.TipoPago.Boleta
                        End With
                        pago = PagosBL.Save(pago)
                        If IsNothing(pago) Then
                            Util.MensajeError("Error al Registrar Pago")
                            Exit Sub
                        End If
                        VentaBL.RecibirPendiente(gGrilla.Item("ventaid", gGrilla.CurrentRow.Index).Value, pago)
                        Util.Mensaje("Pago Recibido Correctamente")
                        Titulo()
                    End If
                End Using
            End If
        End If
    End Sub
End Class